/**
 * Copyright 2012 Pablo Danilo Aliscioni
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package ar.com.pabloaliscioni.cloud.core.configuration;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

/**
 * Represents the configuration deserialized form the XML configuration file for a given provider
 * 
 * @author Pablo Danilo Aliscioni
 * @since 1.0
 */
@XStreamAlias("provider")
public class ProviderConfiguration implements Serializable
{
	/**
	 * Generated Serial Version UID
	 */
	private static final long serialVersionUID = 3804267673518928117L;
	
	/**
	 * Set of configuration items containing the configuration parameters of the given provider
	 */
	@XStreamImplicit(itemFieldName="item",keyFieldName="key")
	private Set<ProviderConfigurationElement> configurationItems;
	
	/**
	 * Name of the class implementing the given provider
	 */
	@XStreamAlias("implementation")
   	@XStreamAsAttribute
	private String implementation;
	
	/**
	 * Name of the current provider
	 */
	@XStreamAlias("name")
   	@XStreamAsAttribute
	private String name;
	
	/**
	 * Non-parameterized constructor
	 */
	public ProviderConfiguration()
	{
		super();
	}
	
	/**
	 * Parameterized constructor
	 * 
	 * @param name Name of the provider
	 * @param implementation Name of the class implementing the current provider
	 * @param configurationItems Set of configuration items containing the configuration parameters of the given provider
	 */
	public ProviderConfiguration(String name, String implementation, Set<ProviderConfigurationElement> configurationItems)
	{
		super();
		
		this.setName(name);
		this.setImplementation(implementation);
		this.setConfigurationItems(configurationItems);
	}
	
	/**
	 * Set of configuration items containing the configuration parameters of the given provider
	 * 
	 * @return The Set of configuration items containing the configuration parameters of the given provider
	 */
	public Set<ProviderConfigurationElement> getConfigurationItems()
	{
		if(configurationItems == null)
		{
			configurationItems = new HashSet<ProviderConfigurationElement>();
		}
		
		return configurationItems;
	}
	/**
	 * Sets the Set of configuration items containing the configuration parameters of the given provider
	 * 
	 * @param configurationItems The Set of configuration items containing the configuration parameters of the given provider
	 * 
	 * @throws IllegalArgumentException If the configuration items are invalid
	 */
	public void setConfigurationItems(Set<ProviderConfigurationElement> configurationItems)
	{
		if(configurationItems == null)
			throw new IllegalArgumentException("The provided configuration items are invalid");
		
		this.configurationItems = configurationItems;
	}
	/**
	 * Adds a new configuration item to the Set of configuration items containing the configuration parameters of the given provider
	 * 
	 * @param configurationItem The configuration item containing the configuration parameters of the given provider
	 * 
	 * @throws IllegalArgumentException If the configuration item is invalid
	 */
	public void addConfigurationItem(ProviderConfigurationElement configurationItem)
	{
		if(configurationItem == null)
			throw new IllegalArgumentException("The provided configuration item is invalid");
		
		getConfigurationItems().add(configurationItem);
	}
	
	/**
	 * Gets the Name of the class implementing the given provider
	 * 
	 * @return The Name of the class implementing the given provider
	 */
	public String getImplementation()
	{
		return implementation;
	}
	/**
	 * Sets the Name of the class implementing the given provider
	 * 
	 * @param implementation The Name of the class implementing the given provider
	 * 
	 * @throws IllegalArgumentException If the implementation is invalid
	 */
	public void setImplementation(String implementation)
	{
		if(implementation == null || implementation.isEmpty())
			throw new IllegalArgumentException("The provided implementation is invalid");
		
		this.implementation = implementation;
	}
	
	/**
	 * Gets the name of the current provider
	 * 
	 * @return The name of the current provider
	 */
	public String getName()
	{
		return name;
	}
	/**
	 * Sets the name of the current provider
	 * 
	 * @param name The name of the current provider
	 * 
	 * @throws IllegalArgumentException If the name is invalid
	 */
	public void setName(String name)
	{
		if(name == null || name.isEmpty())
			throw new IllegalArgumentException("The provided name is invalid");
		
		this.name = name;
	}
	
	/**
	 * Gets the value of a given configuration parameter
	 * 
	 * @param key The key of the parameter to get the value
	 * 
	 * @return The value corresponding to the given configuration parameter
	 * 
	 * @throws IllegalArgumentException If the configuration key is invalid
	 */
	public String getConfigurationValue(String key)
	{
		if(key == null || key.isEmpty())
			throw new IllegalArgumentException("The provided configuration key is invalid");
		
		for (ProviderConfigurationElement item : getConfigurationItems())
		{
			if(item.getKey().equals(key))
				return item.getValue();
		}
		
		return null;
	}
}
